package com.piggy.logi.sys.controller;

import java.util.List;
import java.util.Arrays;

import com.piggy.common.log.annotation.LogiLog;
import com.piggy.common.satoken.utils.SecurityUtils;
import com.piggy.logi.sys.vo.LogiCarrierDriverLineExtVo;
import lombok.RequiredArgsConstructor;

import javax.validation.constraints.*;

import org.springframework.web.bind.annotation.*;

import org.springframework.validation.annotation.Validated;
import com.piggy.common.log.annotation.Log;
import com.piggy.common.core.web.controller.BaseController;
import com.piggy.common.log.enums.BusinessType;
import com.piggy.logi.sys.bo.LogiCarrierDriverLineQueryBo;
import com.piggy.logi.sys.bo.LogiCarrierDriverLineEditBo;
import com.piggy.logi.sys.service.ILogiCarrierDriverLineService;
import com.piggy.common.core.utils.poi.ExcelUtil;
import com.piggy.common.core.web.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.piggy.common.core.domain.R;

import java.lang.Integer;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * 司机常用线路管理Controller
 *
 * @author piggy
 * @date 2023-08-23
 */
@Api(value = "司机常用线路管理控制器", tags = {"司机常用线路管理管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys/driverline")
public class LogiCarrierDriverLineController extends BaseController {

    private final ILogiCarrierDriverLineService iLogiCarrierDriverLineService;

    /**
     * 查询司机常用线路管理列表
     */
    @ApiOperation("查询司机常用线路管理列表")
    //@SaCheckPermission("sys:driverline:list")
    @GetMapping("/list")
    public TableDataInfo<LogiCarrierDriverLineExtVo> list(@Validated LogiCarrierDriverLineQueryBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        return iLogiCarrierDriverLineService.queryPageList(bo);
    }

    /**
     * 导出司机常用线路管理列表
     */
    @ApiOperation("导出司机常用线路管理列表")
    //@SaCheckPermission("sys:driverline:export")
    @LogiLog(title = "司机常用线路管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(@Validated LogiCarrierDriverLineQueryBo bo, HttpServletResponse response) throws IOException {
        List<LogiCarrierDriverLineExtVo> list = iLogiCarrierDriverLineService.queryList(bo);
        ExcelUtil<LogiCarrierDriverLineExtVo> util = new ExcelUtil<LogiCarrierDriverLineExtVo>(LogiCarrierDriverLineExtVo.class);
        util.exportExcel(response, list, "司机常用线路管理");
    }

    /**
     * 获取司机常用线路管理详细信息
     */
    @ApiOperation("获取司机常用线路管理详细信息")
    //@SaCheckPermission("sys:driverline:query")
    @GetMapping("/{id}")
    public R<LogiCarrierDriverLineExtVo> getInfo(@NotNull(message = "主键不能为空")
                                                 @PathVariable("id") String id) {
        return R.ok(iLogiCarrierDriverLineService.queryById(id));
    }

    /**
     * 新增司机常用线路管理
     */
    @ApiOperation("新增司机常用线路管理")
    //@SaCheckPermission("sys:driverline:add")
    @LogiLog(title = "司机常用线路管理", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Integer> add(@Validated @RequestBody LogiCarrierDriverLineEditBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        return R.ok(iLogiCarrierDriverLineService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改司机常用线路管理
     */
    @ApiOperation("修改司机常用线路管理")
    //@SaCheckPermission("sys:driverline:edit")
    @LogiLog(title = "司机常用线路管理", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Integer> edit(@Validated @RequestBody LogiCarrierDriverLineEditBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        return R.ok(iLogiCarrierDriverLineService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 新增司机常用线路管理
     */
    @ApiOperation("批量修改司机常用线路管理")
    //@SaCheckPermission("sys:carline:add")
    @LogiLog(title = "司机常用线路管理", businessType = BusinessType.INSERT)
    @PostMapping("batchEdit")
    public R<Integer> batchEdit(@Validated @RequestBody List<LogiCarrierDriverLineEditBo> boList) {
        Long deptId = SecurityUtils.getDeptId();
        boList.forEach(item -> item.setDeptId(deptId));
        return R.ok(iLogiCarrierDriverLineService.batchByUpdateBo(boList) ? 1 : 0);
    }

    /**
     * 新增司机常用线路管理
     */
    @ApiOperation("批量新增司机常用线路管理")
    //@SaCheckPermission("sys:carline:add")
    @LogiLog(title = "司机常用线路管理", businessType = BusinessType.INSERT)
    @PostMapping("batchAdd")
    public R<Integer> batchAdd(@Validated @RequestBody List<LogiCarrierDriverLineEditBo> boList) {
        Long deptId = SecurityUtils.getDeptId();
        boList.forEach(item -> item.setDeptId(deptId));
        return R.ok(iLogiCarrierDriverLineService.batchByAddBo(boList) ? 1 : 0);
    }

    /**
     * 删除司机常用线路管理
     */
    @ApiOperation("删除司机常用线路管理")
    //@SaCheckPermission("sys:driverline:remove")
    @LogiLog(title = "司机常用线路管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Integer> remove(@NotEmpty(message = "主键不能为空")
                             @PathVariable Long[] ids) {
        return R.ok(iLogiCarrierDriverLineService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
